#include "stdafx.h"
#include "td_manage.h"
#include "td_map.h"
#include "td_enemy.h"
#include "td_tower.h"
#include<vector>

td_manage::td_manage()
{
}
td_manage::~td_manage()
{
}
void td_manage::ready()
{
	resource->input_bing();
	map->ready_map();
	resource->input_tower();
	resource->input_bomb();
	bomb->intput_bombpic(resource->bombpic);
	resource->input_welcomepic();
	resource->input_tower2();
}
void td_manage::create_towervec(int x,int y)
{
	if (map->map[x / 70][y / 70] > 0)return;
	td_tower *tower2 = new td_tower;
	tower2->shoot_scope = 50000;
	tower2->readyxy(x, y);
	tower2->get_tower(resource->towerpic);
	tower2->hurt_tower = 3;
	tower2->shoot_velocity = 4;
	vec_tower.push_back(tower2);
}
void td_manage::create_towervec1(int x, int y)
{
	cout << "asdasd";
	if (map->map[x / 70][y / 70] > 0)return;
	td_tower *tower1 = new td_tower;
	tower1->shoot_scope = 50000;
	tower1->readyxy(x, y);
	tower1->get_tower(resource->towerpic2);
	tower1->hurt_tower = 8;
	tower1->shoot_velocity = 15;
	vec_tower.push_back(tower1);
}
void td_manage::on_mouse(int event, int x, int y, int flags, void* param)
{
	td_manage *img = (td_manage *)param;
	switch (event)
	{
		case CV_EVENT_LBUTTONDOWN:
		{
			if (y < 488 && y>397 && x < 585 && x>388)img->judge_welcome = 1;
		}
	}
}
void td_manage::on_mouse1(int event, int x, int y, int flags, void* param)
{
	td_manage *img = (td_manage *)param;
	switch (event)
	{
		case CV_EVENT_LBUTTONDOWN:
		{
			int k = y % 70, j = x % 70;
			img->create_towervec(y-k,x-j);
			break;
		}
		case CV_EVENT_RBUTTONDOWN:
		{
			int k = y % 70, j = x % 70;
			img->create_towervec1(y - k,x - j);
			break;
		}
	}
}
void td_manage::welcome()
{
	cvShowImage("tower",resource->welcomepic);
	while (true)
	{
		cvWaitKey(100);
		cvSetMouseCallback("tower", on_mouse, this);
		if (judge_welcome == 1)return;
	}
}
void td_manage::game_manage()
{
	cvWaitKey(500);
	copy = cvCloneImage(map->background1);
	for (int i=0;;i++)
	{
		cvSetMouseCallback("tower", on_mouse1, this);
		cvCopy(map->background1,copy);
		if (i  %70==0 )
			create_enemyvec();
		for (int k = 0;k < vec_tower.size();k++)
		{
			for (int j = 0;j < vec_enemy.size();j++)
			{
				if (vec_tower[k]->judge_enemy(vec_enemy[j], resource->bombpic) == 1)
					j = vec_enemy.size();
			}
			vec_tower[k]->shoot_enemy();
		}
		for (int j = 0;j < vec_tower.size();j++)
		{
			vec_tower[j]->judge_bomb();
		}
		for (int j = 0;j < vec_tower.size();j++)
		{
			vec_tower[j]->delete_bomb();
		}
		move(i);
		for (int j = 0;j < vec_tower.size();j++)
		{
			vec_tower[j]->draw_tower(copy);
			vec_tower[j]->drow_bonb(copy);
		}
		/*for (int k = 0;k < vec_tower.size();k++)
		{
			for (int j = 0;j < vec_enemy.size();j++)
			{
				if (vec_tower[k]->judge_enemy(vec_enemy[j], j, copy, resource->bombpic) == 1)
					j = vec_enemy.size();
			}
		}*/
		/*for (int j = 0;j < vec_tower.size();j++)
		{
			vec_tower[j]->judge_bomb();
		}*/
		cvShowImage("tower", copy);
		if (cvWaitKey(20) == 27)break;
	}
	//cvShowImage("tower", copy);
}
void td_manage::release_game()
{
	resource->input_gameover();
	cvShowImage("tower", resource->gameoverpic);
	cvWaitKey(1000);
	cvReleaseImage(&copy);
	cvReleaseImage(&map->background1);
	resource->release();
	exit(0);
}
void td_manage::move(int i)
{
		vector<td_enemy *>::iterator q;
		q = vec_enemy.begin();
		while (q != vec_enemy.end())
		{
			if ((*q)->get_xy().x >= 560 && (*q)->get_xy().y >= 630)release_game();
			if ((*q)->hp_enemy==0)
			{
				delete(*q);
				q = vec_enemy.erase(q);
				continue;
				q--;
			}
			(*q)->dir_enemy = map->getdir_enemy(*q);
			if (q >= vec_enemy.end())
			{
				break;
			}
			(*q)->move_enemy(copy, i);
			q++;
		}
}
void td_manage::create_enemyvec()
{
	td_enemy *enemy1 = new td_enemy;
	enemy1->speed_enemy = 4;
	enemy1->hp_enemy = 50;
	enemy1->input_enemy1(resource->xiaobing1,resource->xiaobing2);
	vec_enemy.push_back(enemy1);
}
